package cn.sofwin.web.blog.web;

import cn.sofwin.web.blog.entity.User;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * 这是过滤器
 * @author ZX
 * @version 1.0
 */
@WebFilter({"/blog/user/*","/blog/article/from","/blog/article/remove","/blog/admin/*"})
public class LoginFilter extends HttpFilter {

    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) throws IOException, ServletException {
        String url=req.getServletPath();
        if ("/blog/user/login".equals(url)||"/blog/user/register".equals(url)||"/blog/user/check".equals(url)||"/blog/admin".equals(url)){
            //向后放行
            chain.doFilter(req,resp);
        }else {
            User user =(User) req.getSession().getAttribute("LOGIN_USER");
            if (user!= null){
                //向后放行
                chain.doFilter(req,resp);
            }else {
                //用户未登录，重定向到登录页面
                resp.sendRedirect("/blog/user/login");
            }
        }

    }
}
